import './header.js';
import { $ } from './library/jquery-module.js';
import { cookie } from './library/cookie.js';
import './addcookie.js';

let keyword = cookie.get('keyword');
if (keyword) {
    $('#search').val(keyword);
    $.ajax({
        type: "get",
        url: "../../interface/getSearch.php",
        data: {
            keyword
        },
        dataType: "json",
        success: function(val) {
            let str = '';
            if (val.length == 0) {
                str = '<h3>对不起，没有找到符合的商家！</h3>';
            } else {
                str = '<h4>智能排序</h4>';
                val.forEach(elm => {
                    let picture = JSON.parse(elm.picture);
                    // console.log(picture);
                    str += `
                <li class="rows clearfix">
                    <div class="col-md-4 img">
                        <img src="../${picture[0].src}" alt="">
                    </div>
                    <div class="col-md-8">
                        <p class="p-title">${elm.title}</p>
                        <p class="p-subtitle">${elm.subtitle}</p>
                        <p class="p-price">￥${elm.price}起</p>
                    </div>
                </li>
                `;
                });
            }
            $('.search-body').html(str);
        }
    });

    cookie.remove('keyword');
}